﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;


namespace DALLayer.Providers
{
    public class ArticleTagDataProvider : DataAccessBase
    {
         #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="ArticleTagDataProvider" /> class.
        /// </summary>
        public ArticleTagDataProvider() { }
        #endregion

        #region [Public Methods]
        /// <summary>
        ///  Gets all the article tags by article id
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetArticleTags(int articleID)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT Tag.Tag");
                base.CommandTextBuilder.Append(@" FROM ((Article INNER JOIN
                         ArticleTag ON Article.ArticleID = ArticleTag.ArticleID) INNER JOIN
                         Tag ON ArticleTag.TagID = Tag.TagID)");         
                //Add the sql string to retrieve data based on ArticleTypeID
                base.CommandTextBuilder.Append(@" WHERE (Article.ArticleID = @ArticleID) AND (Tag.IsActive = True) AND (Article.IsActive = True)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetTagArticleID.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// The method inserts article tag
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <param name="tagID">tagID</param>
        /// <param name="userID">userID</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertArticleTag(int articleID, int tagID, int userID, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO ArticleTag(ArticleID, TagID, UserID, IsActive)
                    VALUES(@ArticleID, @TagID, @UserID, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                base.DBParams.Add(@"@TagID", tagID);
                base.DBParams.Add(@"@UserID", userID);
                base.DBParams.Add(@"@IsActive", isActive);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception eX)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }

        /// <summary>
        /// The method updates article tag
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <param name="tagID">tagID</param>
        /// <param name="userID">userID</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateArticleTag(int articleID, int tagID, int userID, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE ArticleTag
                    SET UserID = @UserID,
                        IsActive = @IsActive
                    WHERE (ArticleID = @ArticleID AND TagID = @TagID)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@UserID", userID);
                base.DBParams.Add(@"@IsActive", isActive);
                base.DBParams.Add(@"@ArticleID", articleID);
                base.DBParams.Add(@"@TagID", tagID);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception eX)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        } 
        #endregion
    }
}
